﻿unit uDictcnDict;

interface

uses
  Sysutils, Classes, Variants, IdURI, uDictTranslate;

type
  TDictcnDict = class(TDictTranslate)
  private
    tmpStringList: TStrings;
  protected
    procedure Init; override;
    function AnalyseResult: Boolean; override;
  public
    constructor Create; override;
    destructor Destroy; override;
  end;

implementation

uses uWebTranslate;

{ TDictcnDict }

const
  cLangInfos: array[0..1] of TLangInfo = (
    (Lcid: 2052; Flag: 'cn'; ChineseName: '中文'),
    (Lcid: 1033; Flag: 'en'; ChineseName: '英文')
  );

constructor TDictcnDict.Create;
begin
  inherited;
  tmpStringList := TStringList.Create;
end;

destructor TDictcnDict.Destroy;
begin
  FreeAndNil(tmpStringList);
  inherited;
end;

procedure TDictcnDict.Init;
var
  idx, iLen: Integer;
begin
  FEngineName := '海词';
//  FTSFormatUrl := 'http://api.dict.cn/wapi.php?q=%s&client=lingoes';
  //FTSFormatUrl := 'http://dict.cn/ws.php?q=%s';
  //FTSFormatUrl := 'http://api.dict.cn/ws.php?utf8=true&q=%s';
  FParamIsUtf8 := True;

  iLen := Length(cLangInfos);
  SetLength(FLangInfos, iLen);
  for idx := 0 to iLen - 1 do
    FLangInfos[idx] := cLangInfos[idx];
end;

(*  <?xml version="1.0" encoding="UTF-8" ?>
- <dict>
  <key>translate</key>
  <lang>ec</lang>
  <audio>http://dict.cn/mp3.php?q=OnjwV</audio>
  <pron>træns'leit</pron>
  <def>v. 翻译，解释，转移，调动</def>
  </dict>
  <DEF>n. 测试, 试验, 考验 vt. 测试, 试验, 接受测验 adj. 测试的</DEF>
  *)
function TDictcnDict.AnalyseResult: Boolean;
var
  mStr: string;
  iPos: Integer;
begin
  Result := False;
  mStr := FIE.Doc.body.outerHTML;
  iPos := Pos('<DEF>', mStr);
  mStr := Copy(mStr, iPos+5, MaxInt);
  iPos := Pos('</DEF>', mStr);
  mStr := Copy(mStr, 1, iPos-1);

  if Trim(mStr)<>'' then
  begin
    SetLength(FDictInfoList, Length(FDictInfoList)+1);
    SetLength(FDictInfoList[Length(FDictInfoList)-1].Explain, 1);
    FDictInfoList[Length(FDictInfoList)-1].Cixing := '';
    FDictInfoList[Length(FDictInfoList)-1].Explain[0] := mStr;

    Result := True;
  end;
end;

initialization
  TWebTranslate.RegisterEngine(TWebTranslate.EngineInfo(TDictcnDict, tsDict, '海词'));

end.
